package com.homegoods.videomodule.ui.fragments;

import android.app.Activity;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.homegoods.resource.dialog.BaseListBottomSheetDialog;
import com.homegoods.videomodule.R;
import com.homegoods.videomodule.databinding.VideoAllCommentHeadLayoutBinding;
import com.homegoods.videomodule.model.CommentModel;
import com.homegoods.videomodule.presenter.CommentPresenter;
import com.homegoods.videomodule.ui.activity.SendCommentActivity;
import com.homegoods.videomodule.ui.view.OnPublishCommentListener;

/**
 * @author QiQi
 * @ClassName: AllCommentListDialogFragment
 * @date 2018/10/23
 * @description
 */
public class AllCommentListDialogFragment extends BaseListBottomSheetDialog<CommentPresenter> implements OnPublishCommentListener {


    protected int getStyle() {
        return R.style.BottomSheetDialogStyle2;
    }

    @Override
    public void onStart() {
        super.onStart();
        getDialog().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);
    }

    @Override
    protected int layoutResId() {
        return R.layout.video_fragment_comment;
    }

    @Override
    protected void initView(View view) {
        super.initView(view);
        TextView mComment = view.findViewById(R.id.tv_comment);
        mComment.setOnClickListener(v -> startActivityForResult(new Intent(getContext(), SendCommentActivity.class), 100));
        setTitle("全部回复");

        adapter.setOnItemClickListener((v, iItem) -> {
            startActivityForResult(new Intent(getContext(), SendCommentActivity.class), 100);
        });
    }


    @Override
    protected void loadData(Bundle arguments) {
        CommentModel model = arguments.getParcelable("data");
        VideoAllCommentHeadLayoutBinding binding = DataBindingUtil.inflate(LayoutInflater.from(getContext()), R.layout.video_all_comment_head_layout, null, false);
        binding.setItem(model);
        binding.tvReport.setOnClickListener(v -> {
            //举报
            ReportDialogFragment reportDialogFragment = new ReportDialogFragment();
            reportDialogFragment.showDialog(getFragmentManager(), "report");
        });
        mList.addHeaderView(binding.getRoot());
        super.loadData(arguments);
    }

    @Override
    public CommentPresenter getPresenter() {
        return new CommentPresenter(this,"");
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != Activity.RESULT_OK || data == null) {
            return;
        }

        if (requestCode == 100) {
            String comment = data.getStringExtra("result");
            publishComment(comment);
        }
    }

    @Override
    public void publishComment(String comment) {
        mList.smoothScrollToPosition(0);
        mPresenter.publishComment(comment);
    }
}
